#include "etcd.hpp"
#include "channel.hpp"
#include "utils.hpp"
#include <gflags/gflags.h>
#include <gtest/gtest.h>
#include <thread>
#include "user.pb.h"
#include "base.pb.h"

DEFINE_bool(run_mode, false, "程序的运行模式,false-调试； true-发布；");
DEFINE_string(log_file, "", "发布模式下,用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下,用于指定日志输出等级");

DEFINE_string(etcd_host, "http://127.0.0.1:2379", "服务注册中心地址");
DEFINE_string(base_service, "/service", "服务监控根目录");
DEFINE_string(user_service, "/service/user_service", "服务监控根目录");

ServiceChannelManager::ptr _userService_channels;
my::UserInfo user_info;

std::string login_ssid;
std::string new_nickname = "亲爱的猪妈妈";

// TEST(用户子服务测试, 用户注册测试) 
// {
//     auto channel = _userService_channels->choose(FLAGS_user_service);//获取通信信道
//     ASSERT_TRUE(channel);
//     user_info.set_nickname("猪妈妈");

//     my::UserRegisterReq req;
//     req.set_request_id(my::uuid());
//     req.set_nickname(user_info.nickname());
//     req.set_password("123456");
//     my::UserRegisterRsp rsp;
//     brpc::Controller cntl;
//     my::UserService_Stub stub(channel.get());
//     stub.UserRegister(&cntl, &req, &rsp, nullptr);
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(rsp.success());
// }

TEST(用户子服务测试, 用户登录测试) 
{
    auto channel = _userService_channels->choose(FLAGS_user_service);//获取通信信道
    ASSERT_TRUE(channel);

    my::UserLoginReq req;
    req.set_request_id(my::uuid());
    req.set_nickname("亲爱的猪妈妈");
    req.set_password("123456");
    my::UserLoginRsp rsp;
    brpc::Controller cntl;
    my::UserService_Stub stub(channel.get());
    stub.UserLogin(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
    login_ssid = rsp.login_session_id();
}

TEST(用户子服务测试, 用户头像设置测试) {
    auto channel = _userService_channels->choose(FLAGS_user_service);//获取通信信道
    ASSERT_TRUE(channel);

    my::SetUserAvatarReq req;
    req.set_request_id(my::uuid());
    req.set_user_id(user_info.user_id());
    req.set_session_id(login_ssid);
    req.set_avatar(user_info.avatar());
    my::SetUserAvatarRsp rsp;
    brpc::Controller cntl;
    my::UserService_Stub stub(channel.get());
    stub.SetUserAvatar(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
}

TEST(用户子服务测试, 用户签名设置测试) {
    auto channel = _userService_channels->choose(FLAGS_user_service);//获取通信信道
    ASSERT_TRUE(channel);

    my::SetUserDescriptionReq req;
    req.set_request_id(my::uuid());
    req.set_user_id(user_info.user_id());
    req.set_session_id(login_ssid);
    req.set_description(user_info.description());
    my::SetUserDescriptionRsp rsp;
    brpc::Controller cntl;
    my::UserService_Stub stub(channel.get());
    stub.SetUserDescription(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
}

TEST(用户子服务测试, 用户昵称设置测试) {
    auto channel = _userService_channels->choose(FLAGS_user_service);//获取通信信道
    ASSERT_TRUE(channel);

    my::SetUserNicknameReq req;
    req.set_request_id(my::uuid());
    req.set_user_id(user_info.user_id());
    req.set_session_id(login_ssid);
    req.set_nickname(new_nickname);
    my::SetUserNicknameRsp rsp;
    brpc::Controller cntl;
    my::UserService_Stub stub(channel.get());
    stub.SetUserNickname(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
}

TEST(用户子服务测试, 用户信息获取测试) {
    auto channel = _userService_channels->choose(FLAGS_user_service);//获取通信信道
    ASSERT_TRUE(channel);

    my::GetUserInfoReq req;
    req.set_request_id(my::uuid());
    req.set_user_id(user_info.user_id());
    req.set_session_id(login_ssid);
    my::GetUserInfoRsp rsp;
    brpc::Controller cntl;
    my::UserService_Stub stub(channel.get());
    stub.GetUserInfo(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
    ASSERT_EQ(user_info.user_id(), rsp.user_info().user_id());
    ASSERT_EQ(new_nickname, rsp.user_info().nickname());
    ASSERT_EQ(user_info.description(), rsp.user_info().description());
    ASSERT_EQ("", rsp.user_info().phone());
    ASSERT_EQ(user_info.avatar(), rsp.user_info().avatar());
}

void set_user_avatar(const std::string &uid, const std::string &avatar) {
    auto channel = _userService_channels->choose(FLAGS_user_service);//获取通信信道
    ASSERT_TRUE(channel);
    my::SetUserAvatarReq req;
    req.set_request_id(my::uuid());
    req.set_user_id(uid);
    req.set_session_id(login_ssid);
    req.set_avatar(avatar);
    my::SetUserAvatarRsp rsp;
    brpc::Controller cntl;
    my::UserService_Stub stub(channel.get());
    stub.SetUserAvatar(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
}


TEST(用户子服务测试, 批量用户信息获取测试) {
    set_user_avatar("用户ID1", "小猪佩奇的头像数据");
    set_user_avatar("用户ID2", "小猪乔治的头像数据");
    auto channel = _userService_channels->choose(FLAGS_user_service);//获取通信信道
    ASSERT_TRUE(channel);

    my::GetMultiUserInfoReq req;
    req.set_request_id(my::uuid());
    req.add_users_id("用户ID1");
    req.add_users_id("用户ID2");
    req.add_users_id("ee55-9043bfd7-0001");
    my::GetMultiUserInfoRsp rsp;
    brpc::Controller cntl;
    my::UserService_Stub stub(channel.get());
    stub.GetMultiUserInfo(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
    auto users_map = rsp.mutable_users_info();
    my::UserInfo fuser = (*users_map)["ee55-9043bfd7-0001"];
    ASSERT_EQ(fuser.user_id(), "ee55-9043bfd7-0001");
    ASSERT_EQ(fuser.nickname(), "猪爸爸");
    ASSERT_EQ(fuser.description(), "");
    ASSERT_EQ(fuser.phone(), "");
    ASSERT_EQ(fuser.avatar(), "");

    my::UserInfo puser = (*users_map)["用户ID1"];
    ASSERT_EQ(puser.user_id(), "用户ID1");
    ASSERT_EQ(puser.nickname(), "小猪佩奇");
    ASSERT_EQ(puser.description(), "这是一只小猪");
    ASSERT_EQ(puser.phone(), "手机号1");
    ASSERT_EQ(puser.avatar(), "小猪佩奇的头像数据");
    
    my::UserInfo quser = (*users_map)["用户ID2"];
    ASSERT_EQ(quser.user_id(), "用户ID2");
    ASSERT_EQ(quser.nickname(), "小猪乔治");
    ASSERT_EQ(quser.description(), "这是一只小小猪");
    ASSERT_EQ(quser.phone(), "手机号2");
    ASSERT_EQ(quser.avatar(), "小猪乔治的头像数据");
}

int main(int argc, char *argv[])
{
    google::ParseCommandLineFlags(&argc, &argv, true);
    initLogger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);

    //1. 先构造Rpc信道管理对象
    _userService_channels = std::make_shared<ServiceChannelManager>();
    _userService_channels->declared(FLAGS_user_service);
    auto put_cb = std::bind(&ServiceChannelManager::onServiceUpline, _userService_channels.get(), std::placeholders::_1, std::placeholders::_2);
    auto del_cb = std::bind(&ServiceChannelManager::onServiceDownline, _userService_channels.get(), std::placeholders::_1, std::placeholders::_2);
    
    //2. 构造服务发现对象
    my::Discovery::ptr dclient = std::make_shared<my::Discovery>(FLAGS_etcd_host, FLAGS_base_service, put_cb, del_cb);
    
    user_info.set_nickname("猪妈妈");
    user_info.set_user_id("1d56-513d8e49-0002");
    user_info.set_description("这是一个美丽的猪妈妈");
    user_info.set_phone("15929917272");
    user_info.set_avatar("猪妈妈头像数据");
    testing::InitGoogleTest(&argc, argv);
    LOG_DEBUG("开始测试！");
    return RUN_ALL_TESTS();
}